﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYRevitCode.FormUtility;

namespace YArchitech.Plumbing
{
	public partial class FormCreateThwartwisePipe : System.Windows.Forms.Form
	{
		public FormCreateThwartwisePipe(Document doc, List<string> levelUp, List<string> pipeType, List<string> pipingSystemType)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{B5D06787-1C6D-4B14-A488-BAE7299B8AB1}", null);
			this.doc = doc;
			this.levelUp = levelUp;
			this.pipeType = pipeType;
			this.pipingSystemType = pipingSystemType;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
		}

		private void FormCreateThwartwisePipe_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			foreach (string item in this.pipeType)
			{
				this.comboBoxPipeClass.Items.Add(item);
			}
			foreach (string item2 in this.pipingSystemType)
			{
				this.comboBoxPipingSystemType.Items.Add(item2);
			}
			foreach (string item3 in this.levelUp)
			{
				this.comboBoxLevel.Items.Add(item3);
			}
			this.GetUserData();
		}

		private void GetUserData()
		{
			string[] thwartwisePipeText = this.userSetting.GetThwartwisePipeText();
			this.comboBoxPipeClass.Text = thwartwisePipeText[0];
			this.comboBoxPipingSystemType.Text = thwartwisePipeText[1];
			if (string.IsNullOrEmpty(this.comboBoxPipeClass.Text) && this.comboBoxPipeClass.Items != null && this.comboBoxPipeClass.Items.Count > 0)
			{
				this.comboBoxPipeClass.SelectedItem = this.comboBoxPipeClass.Items[0];
			}
			if (string.IsNullOrEmpty(this.comboBoxPipingSystemType.Text) && this.comboBoxPipingSystemType.Items != null && this.comboBoxPipingSystemType.Items.Count > 0)
			{
				this.comboBoxPipingSystemType.SelectedItem = this.comboBoxPipingSystemType.Items[0];
			}
			this.comboBoxPipeDiameter.Items.Clear();
			foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.comboBoxPipeClass.Text).GetSizes())
			{
				this.comboBoxPipeDiameter.Items.Add(Math.Round(304.8 * mepsize.NominalDiameter, MidpointRounding.AwayFromZero).ToString());
			}
			this.comboBoxPipeDiameter.Text = thwartwisePipeText[2];
			if (string.IsNullOrEmpty(this.comboBoxPipeDiameter.Text) && this.comboBoxPipeDiameter.Items != null && this.comboBoxPipeDiameter.Items.Count > 0)
			{
				this.comboBoxPipeDiameter.SelectedItem = this.comboBoxPipeDiameter.Items[0];
				thwartwisePipeText[2] = this.comboBoxPipeDiameter.Text;
			}
			this.textBoxLevel.Text = thwartwisePipeText[3];
			if (string.IsNullOrEmpty(this.textBoxLevel.Text))
			{
				this.textBoxLevel.Text = "0.0";
				thwartwisePipeText[3] = "0.0";
			}
			this.textBoxOffset.Text = thwartwisePipeText[4];
			if (string.IsNullOrEmpty(this.textBoxOffset.Text))
			{
				this.textBoxOffset.Text = "0.0";
				thwartwisePipeText[4] = "0.0";
			}
			this.hyTextBox1.Text = thwartwisePipeText[5];
			if (string.IsNullOrEmpty(this.hyTextBox1.Text))
			{
				this.hyTextBox1.Text = "0.0";
				thwartwisePipeText[5] = "0.0";
			}
			this.textDiameter = thwartwisePipeText[2];
			this.textOffset = thwartwisePipeText[4];
			this.textLevel = thwartwisePipeText[3];
			this.comboBoxLevel.Text = this.doc.ActiveView.GenLevel.Name;
		}

		private void SetUserData()
		{
			string[] array = new string[6];
			array[2] = this.comboBoxPipeDiameter.Text;
			array[3] = this.textBoxLevel.Text;
			array[4] = this.textBoxOffset.Text;
			array[0] = this.comboBoxPipeClass.Text;
			array[1] = this.comboBoxPipingSystemType.Text;
			array[5] = this.hyTextBox1.Text;
			this.userSetting.WriteThwartwisePipeText(array);
		}

		private double zCoord(double offset)
		{
			return offset / 0.3048;
		}

		public PipeType Get_PipeType(Document document, string pipeTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), document);
			return this.GetElementSameName(elementByClass, pipeTypeName) as PipeType;
		}

		public PipingSystemType Get_PipingSystemType(Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as PipingSystemType;
		}

		private Level Get_Level(Document document)
		{
			return document.ActiveView.GenLevel;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			if (this.textBoxLevel.Text != "" && this.textBoxOffset.Text != "" && this.comboBoxPipeDiameter.Text != "" && this.hyTextBox1.Text != "")
			{
				double num = double.Parse(this.textBoxLevel.Text) * 1000.0 + this.Get_Level(this.doc).ProjectElevation * 304.8;
				double offset = double.Parse(this.textBoxOffset.Text);
				double num2 = double.Parse(this.hyTextBox1.Text);
				PipeType pipeType = this.Get_PipeType(this.doc, this.comboBoxPipeClass.Text);
				PipingSystemType pipingSystemType = this.Get_PipingSystemType(this.doc, this.comboBoxPipingSystemType.Text);
				this.level = num;
				this.Offset = offset;
				this.slope = num2;
				this.pipeTypeRiser = pipeType;
				this.pipingSystemTypeRiser = pipingSystemType;
				this.diameter = this.comboBoxPipeDiameter.Text;
				this.SetUserData();
				base.DialogResult = DialogResult.OK;
				return;
			}
			MessageBox.Show("偏移量,直径与相对标高都不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void FormCreateThwartwisePipe_Deactivate(object sender, EventArgs e)
		{
			if (!Regex.IsMatch(this.comboBoxPipeDiameter.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("直径必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.comboBoxPipeDiameter.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxOffset.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("水平偏移必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffset.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxLevel.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("相对标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxLevel.Focus();
				return;
			}
			if (this.isDra && !Regex.IsMatch(this.hyTextBox1.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("坡度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.hyTextBox1.Focus();
				return;
			}
			if (!(this.textBoxLevel.Text != "") || !(this.textBoxOffset.Text != "") || !(this.comboBoxPipeDiameter.Text != "") || !(this.hyTextBox1.Text != ""))
			{
				MessageBox.Show("偏移量,直径与相对标高都不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				if (this.textBoxLevel.Text == "")
				{
					this.textBoxLevel.Text = this.textLevel;
					this.textBoxLevel.Focus();
				}
				if (this.comboBoxPipeDiameter.Text == "")
				{
					this.comboBoxPipeDiameter.Text = this.textDiameter;
					this.comboBoxPipeDiameter.Focus();
				}
				if (this.textBoxOffset.Text == "")
				{
					this.textBoxOffset.Text = this.textOffset;
					this.textBoxOffset.Focus();
				}
				return;
			}
			if (double.Parse(this.comboBoxPipeDiameter.Text.Trim()) < 5.0 || double.Parse(this.comboBoxPipeDiameter.Text.Trim()) > 5000.0)
			{
				MessageBox.Show("直径应大于5mm，小于5000mm", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.comboBoxPipeDiameter.Focus();
				this.comboBoxPipeDiameter.Text = this.textDiameter;
			}
			if (Math.Abs(double.Parse(this.textBoxOffset.Text.Trim())) > 50000.0)
			{
				MessageBox.Show("水平偏移的绝对值不能超过50000mm", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.textBoxOffset.Focus();
				this.textBoxOffset.Text = this.textOffset;
			}
			if (Math.Abs(double.Parse(this.textBoxLevel.Text.Trim())) > 50.0)
			{
				MessageBox.Show("相对标高的绝对值不能超过50m", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.textBoxLevel.Focus();
				this.textBoxLevel.Text = this.textLevel;
				return;
			}
			double num = double.Parse(this.textBoxLevel.Text) * 1000.0 + this.Get_Level(this.doc).ProjectElevation * 304.8;
			double offset = double.Parse(this.textBoxOffset.Text);
			double num2 = double.Parse(this.hyTextBox1.Text);
			PipeType pipeType = this.Get_PipeType(this.doc, this.comboBoxPipeClass.Text);
			PipingSystemType pipingSystemType = this.Get_PipingSystemType(this.doc, this.comboBoxPipingSystemType.Text);
			this.level = num;
			this.Offset = offset;
			if (this.isDra)
			{
				this.slope = num2;
			}
			else
			{
				this.slope = 0.0;
			}
			this.pipeTypeRiser = pipeType;
			this.pipingSystemTypeRiser = pipingSystemType;
			this.diameter = this.comboBoxPipeDiameter.Text;
			this.SetUserData();
			this.textLevel = this.textBoxLevel.Text.Trim();
			this.textOffset = this.textBoxOffset.Text.Trim();
			this.textDiameter = this.comboBoxPipeDiameter.Text.Trim();
		}

		private void comboBoxPipingSystemType_SelectedIndexChanged(object sender, EventArgs e)
		{
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
            if (this.Get_PipingSystemType(this.doc, this.comboBoxPipingSystemType.Text.ToString()).SystemClassification != (MEPSystemClassification)16)
			{
				this.isDra = false;
				this.hyTextBox1.Visible = false;
				this.label6.Visible = false;
				base.Size = new Size(Convert.ToInt32(212.0 * num), Convert.ToInt32(178.0 * num2));
				return;
			}
            if (this.Get_PipingSystemType(this.doc, this.comboBoxPipingSystemType.Text.ToString()).SystemClassification == (MEPSystemClassification)16)
			{
				this.isDra = true;
				this.hyTextBox1.Visible = true;
				this.label6.Visible = true;
				base.Size = new Size(Convert.ToInt32(212.0 * num), Convert.ToInt32(209.0 * num2));
			}
		}

		private void comboBoxPipeClass_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.comboBoxPipeDiameter.Items.Clear();
			foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.comboBoxPipeClass.Text).GetSizes())
			{
				this.comboBoxPipeDiameter.Items.Add(Math.Round(304.8 * mepsize.NominalDiameter, MidpointRounding.AwayFromZero).ToString());
			}
			this.comboBoxPipeDiameter.SelectedIndex = 0;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormCreateThwartwisePipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FormCreateThwartwisePipe.SendMessage(ThwartwiseCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FormCreateThwartwisePipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormCreateThwartwisePipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormCreateThwartwisePipe_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormCreateThwartwisePipe.SendMessage(ThwartwiseCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormCreateThwartwisePipe_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormCreateThwartwisePipe.SendMessage(ThwartwiseCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormCreateThwartwisePipe_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FormCreateThwartwisePipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.SetUserData();
		}

		private void comboBoxPipingSystemType_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBoxPipingSystemType);
		}

		private void AdjustComboBoxDropDownListWidth(object comboBox)
		{
			Graphics graphics = null;
			try
			{
				ComboBox comboBox2 = null;
				if (comboBox is ComboBox)
				{
					comboBox2 = (ComboBox)comboBox;
				}
				else
				{
					if (!(comboBox is ToolStripComboBox))
					{
						return;
					}
					comboBox2 = ((ToolStripComboBox)comboBox).ComboBox;
				}
				int num = comboBox2.Width;
				graphics = comboBox2.CreateGraphics();
				Font font = comboBox2.Font;
				int num2 = (comboBox2.Items.Count > comboBox2.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
				foreach (object obj in comboBox2.Items)
				{
					if (obj != null)
					{
						int num3 = (int)graphics.MeasureString(obj.ToString().Trim(), font).Width + num2;
						if (num < num3)
						{
							num = num3;
						}
					}
				}
				comboBox2.DropDownWidth = num;
			}
			catch
			{
			}
			finally
			{
				if (graphics != null)
				{
					graphics.Dispose();
				}
			}
		}

		private void textBoxLevel_Enter(object sender, EventArgs e)
		{
		}

		private void textBoxOffset_Enter(object sender, EventArgs e)
		{
		}

		private void comboBoxPipeClass_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBoxPipeClass);
		}

		private Document doc;

		private List<string> levelUp = new List<string>();

		private List<string> pipeType = new List<string>();

		private List<string> pipingSystemType = new List<string>();

		public double level;

		public double Offset;

		public double slope;

		public PipeType pipeTypeRiser;

		public PipingSystemType pipingSystemTypeRiser;

		public string diameter = "0";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private OperateSegmentFitting osf;

		private ControlInfoManager m_thisManger;

		private bool isDra = true;

		private string textDiameter = "100";

		private string textLevel = "1.0";

		private string textOffset = "0";
	}
}
